using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

namespace Havivi.FootballPool.Features.Havivi.FootballPool
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("ab8ab3e1-bb72-4bb7-b4ac-2882372ca925")]
    public class HaviviEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            var site = properties.Feature.Parent as SPSite;
            if (site == null) return;
            var web = site.RootWeb;
            CheckIfAssetsListExists(web);
            CreateMatchesList(web);
            CreateUserMatches(web);
            CreateUserStanding(web);
            CreateLabelsList(web);
        }

        private void CreateLabelsList(SPWeb web)
        {
            SPList list = web.Lists.TryGetList("Euro 2012 Labels");
            if (list == null)
            {
                Guid listId = web.Lists.Add("Euro 2012 Labels", "", SPListTemplateType.GenericList);
                list = web.Lists[listId];
                list.Fields.Add("Value", SPFieldType.Text, true);
                AddLabels(list);
            }
            else
            {
                if (!list.Fields.ContainsField("Value"))
                    list.Fields.Add("Value", SPFieldType.Text, false);
                list = web.Lists[list.ID];
            }
            SPField field = list.Fields.GetFieldByInternalName("Title");
            field.ReadOnlyField = true;
            field.Update();
            list.OnQuickLaunch = false;
            list.Update();
            
        }

        private void AddLabels(SPList list)
        {
            SPListItem item = list.Items.Add();
            item["Title"] = "Euro2012Matches";
            item["Value"] = "Euro 2012 Matches";
            item.Update();

            item = list.Items.Add();
            item["Title"] = "MyPredictions";
            item["Value"] = "My Predictions";
            item.Update();

            item = list.Items.Add();
            item["Title"] = "Predictions";
            item["Value"] = "Predictions";
            item.Update();


            item = list.Items.Add();
            item["Title"] = "Rules";
            item["Value"] = "Rules";
            item.Update();

            item = list.Items.Add();
            item["Title"] = "License";
            item["Value"] = "License";
            item.Update();

            item = list.Items.Add();
            item["Title"] = "PoweredBy";
            item["Value"] = "Powered by";
            item.Update();

            item = list.Items.Add();
            item["Title"] = "TeamA";
            item["Value"] = "Team A";
            item.Update();

            item = list.Items.Add();
            item["Title"] = "TeamB";
            item["Value"] = "Team B";
            item.Update();

            item = list.Items.Add();
            item["Title"] = "Score";
            item["Value"] = "Score";
            item.Update();

            item = list.Items.Add();
            item["Title"] = "Points";
            item["Value"] = "Points";
            item.Update();

            item = list.Items.Add();
            item["Title"] = "FirstRound";
            item["Value"] = "First Round";
            item.Update();

            item = list.Items.Add();
            item["Title"] = "Final";
            item["Value"] = "Final";
            item.Update();

            item = list.Items.Add();
            item["Title"] = "SemiFinals";
            item["Value"] = "Semi Finals";
            item.Update();

            item = list.Items.Add();
            item["Title"] = "QuarterFinals";
            item["Value"] = "Quarter Finals";
            item.Update();

            item = list.Items.Add();
            item["Title"] = "Save";
            item["Value"] = "Save";
            item.Update();

            item = list.Items.Add();
            item["Title"] = "Time";
            item["Value"] = "Time";
            item.Update();

          
        }

        private void CheckIfAssetsListExists(SPWeb web)
        {
            try
            {
                web.Lists.EnsureSiteAssetsLibrary();
            }
            catch
            {
                //Sandbox throw error
            }

        }

        private static void CreateUserStanding(SPWeb web)
        {
            SPList list = web.Lists.TryGetList("Euro 2012 Standings");
            if (list == null)
            {
                Guid listId = web.Lists.Add("Euro 2012 Standings", "", SPListTemplateType.GenericList);
                list = web.Lists[listId];
                list.Fields.Add("Position", SPFieldType.Number, false);
                list.Fields.Add("Name", SPFieldType.Text, false);
                list.Fields.Add("Score", SPFieldType.Number, false);
                list.Fields.Add("User", SPFieldType.Text, false);
            }
            else
            {
                //list.Delete();
                //Guid listId = web.Lists.Add("Euro 2012 Standings", "", SPListTemplateType.GenericList);
                list = web.Lists[list.ID];
            }


            SPField fieldScore = list.Fields["Score"];
            fieldScore.DefaultValue = "0";
            fieldScore.Update();

            SPField fieldUsername = list.Fields["User"];
            fieldUsername.ShowInViewForms = false;

            fieldUsername.Update();

            SPField field = list.Fields["Name"];
            SPView defaultView = list.DefaultView;
            defaultView.ViewFields.DeleteAll();
            defaultView.ViewFields.Add(field);
            field = list.Fields["Score"];
            defaultView.ViewFields.Add(field);
            defaultView.Update();

            list.Hidden = true;
            list.DisableGridEditing = true;
            DisabeldField(list, "Name");
            DisabeldField(list, "Score");
            DisabeldField(list, "User");
            DisabeldField(list, "Position");
            SPView view = list.DefaultView;
            view.Query =
                "<OrderBy><FieldRef Name=\"Score\" Ascending=\"False\" /><FieldRef Name=\"Name\" Ascending=\"True\" /></OrderBy>";
            list.Update();

        }

        private static void CreateUserMatches(SPWeb web)
        {
            SPList list = web.Lists.TryGetList("HaviviUserMatches");
            if (list == null)
            {
                Guid listId = web.Lists.Add("HaviviUserMatches", "", SPListTemplateType.GenericList);
                list = web.Lists[listId];
                list.Fields.Add("User", SPFieldType.Text, false);
                list.Fields.Add("DispName", SPFieldType.Text, false);
                list.Fields.Add("Match", SPFieldType.Number, false);
                list.Fields.Add("ScoreTeamA", SPFieldType.Number, false);
                list.Fields.Add("ScoreTeamB", SPFieldType.Number, false);
                list.Fields.Add("Score", SPFieldType.Number, false);
            }
            else
            {
                if (!list.Fields.ContainsField("DispName"))
                    list.Fields.Add("DispName", SPFieldType.Text, false);
                list = web.Lists[list.ID];
            }

            list.Hidden = true;
            list.DisableGridEditing = true;
            DisabeldField(list, "User");
            DisabeldField(list, "Match");
            DisabeldField(list, "ScoreTeamA");
            DisabeldField(list, "ScoreTeamB");
            DisabeldField(list, "Score");
            DisabeldField(list, "DispName");
            list.Update();
        }

        private static void CreateMatchesList(SPWeb web)
        {
            SPList list = web.Lists.TryGetList("HaviviMatches");
            if (list == null)
            {
                Guid listId = web.Lists.Add("HaviviMatches", "", SPListTemplateType.GenericList);
                list = web.Lists[listId];
                list.Fields.Add("TeamA", SPFieldType.Text, false);
                list.Fields.Add("TeamB", SPFieldType.Text, false);
                list.Fields.Add("ScoreA", SPFieldType.Number, false);
                list.Fields.Add("ScoreB", SPFieldType.Number, false);
                list.Fields.Add("FlagA", SPFieldType.Text, false);
                list.Fields.Add("FlagB", SPFieldType.Text, false);
                list.Fields.Add("MatcheDate", SPFieldType.DateTime, false);
                list.Fields.Add("Info", SPFieldType.Text, false);
                list.Fields.Add("Disabled", SPFieldType.Boolean, false);
                list.Update();
                try
                {
                    AddMatchesData(list);
                }
                catch 
                { 
                    //error in non-english enviroment ?
                }
                
            }
            else
            {
                if (!list.Fields.ContainsField("Disabled"))
                    list.Fields.Add("Disabled", SPFieldType.Boolean, false);
                list = web.Lists[list.ID];
            }



            list.Hidden = true;
            list.DisableGridEditing = true;
            DisabeldField(list, "TeamA");
            DisabeldField(list, "TeamB");
            DisabeldField(list, "ScoreA");
            DisabeldField(list, "ScoreB");
            DisabeldField(list, "FlagA");
            DisabeldField(list, "FlagB");
            DisabeldField(list, "MatcheDate");
            DisabeldField(list, "Info");
            list.Update();
        }

        private static void DisabeldField(SPList list, string name)
        {
            SPField field = list.Fields[name];
            field.ShowInViewForms = false;
            field.ShowInNewForm = false;
            field.ShowInEditForm = false;
            field.ShowInListSettings = false;
            field.ReadOnlyField = true;
            field.Update();
        }

        private static void AddMatchesData(SPList list)
        {

            SPListItem item = list.Items.Add();
            item["TeamA"] = "Poland";
            item["FlagA"] = "pol.png";
            item["TeamB"] = "Greece";
            item["FlagB"] = "gre.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-08T16:00:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Russia";
            item["FlagA"] = "rus.png";
            item["TeamB"] = "Czech Republic";
            item["FlagB"] = "cze.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-08T18:45:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Netherlands";
            item["FlagA"] = "net.png";
            item["TeamB"] = "Denmark";
            item["FlagB"] = "den.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-09T16:00:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Germany";
            item["FlagA"] = "ger.png";
            item["TeamB"] = "Portugal";
            item["FlagB"] = "por.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-09T18:45:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Spain";
            item["FlagA"] = "spa.png";
            item["TeamB"] = "Italy";
            item["FlagB"] = "ita.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-10T16:00:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Rep of Ireland";
            item["FlagA"] = "rep.png";
            item["TeamB"] = "Croatia";
            item["FlagB"] = "cro.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-10T18:45:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "France";
            item["FlagA"] = "fra.png";
            item["TeamB"] = "England";
            item["FlagB"] = "eng.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-11T16:00:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Ukraine";
            item["FlagA"] = "ukr.png";
            item["TeamB"] = "Sweden";
            item["FlagB"] = "swe.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-11T18:45:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Greece";
            item["FlagA"] = "gre.png";
            item["TeamB"] = "Czech Republic";
            item["FlagB"] = "cze.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-12T16:00:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Poland";
            item["FlagA"] = "pol.png";
            item["TeamB"] = "Russia";
            item["FlagB"] = "rus.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-12T18:45:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Denmark";
            item["FlagA"] = "den.png";
            item["TeamB"] = "Portugal";
            item["FlagB"] = "por.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-13T16:00:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Netherlands";
            item["FlagA"] = "net.png";
            item["TeamB"] = "Germany";
            item["FlagB"] = "ger.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-13T18:45:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Italy";
            item["FlagA"] = "ita.png";
            item["TeamB"] = "Croatia";
            item["FlagB"] = "cro.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-14T16:00:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Spain";
            item["FlagA"] = "spa.png";
            item["TeamB"] = "Rep of Ireland";
            item["FlagB"] = "rep.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-14T18:45:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Sweden";
            item["FlagA"] = "swe.png";
            item["TeamB"] = "England";
            item["FlagB"] = "eng.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-15T16:00:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Ukraine";
            item["FlagA"] = "ukr.png";
            item["TeamB"] = "France";
            item["FlagB"] = "fra.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-15T18:45:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Czech Republic";
            item["FlagA"] = "cze.png";
            item["TeamB"] = "Poland";
            item["FlagB"] = "pol.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-16T18:45:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Greece";
            item["FlagA"] = "gre.png";
            item["TeamB"] = "Russia";
            item["FlagB"] = "rus.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-16T18:45:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Portugal";
            item["FlagA"] = "por.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-17T18:45:00Z");
            item["TeamB"] = "Netherlands";
            item["FlagB"] = "net.png";
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Denmark";
            item["FlagA"] = "den.png";
            item["TeamB"] = "Germany";
            item["FlagB"] = "ger.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-17T18:45:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Croatia";
            item["FlagA"] = "cro.png";
            item["TeamB"] = "Spain";
            item["FlagB"] = "spa.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-18T18:45:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Italy";
            item["FlagA"] = "ita.png";
            item["TeamB"] = "Rep of Ireland";
            item["FlagB"] = "rep.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-18 18:45:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "England";
            item["FlagA"] = "eng.png";
            item["TeamB"] = "Ukraine";
            item["FlagB"] = "ukr.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-19T18:45:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Sweden";
            item["FlagA"] = "swe.png";
            item["TeamB"] = "France";
            item["FlagB"] = "fra.png";
            item["MatcheDate"] = DateTime.Parse("2012-06-19T18:45:00Z");
            item.Update();

            //QF

            item = list.Items.Add();
            item["TeamA"] = "Group A Winner";
            item["FlagA"] = "/_layouts/images/hhelp.gif";
            item["TeamB"] = "Group B Runner-up";
            item["FlagB"] = "/_layouts/images/hhelp.gif";
            item["MatcheDate"] = DateTime.Parse("2012-06-21T18:45:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Group B Winner";
            item["FlagA"] = "/_layouts/images/hhelp.gif";
            item["TeamB"] = "Group A Runner-up";
            item["FlagB"] = "/_layouts/images/hhelp.gif";
            item["MatcheDate"] = DateTime.Parse("2012-06-22T18:45:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Group C Winner";
            item["FlagA"] = "/_layouts/images/hhelp.gif";
            item["TeamB"] = "Group D Runner-up";
            item["FlagB"] = "/_layouts/images/hhelp.gif";
            item["MatcheDate"] = DateTime.Parse("2012-06-23T18:45:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Group D Winner";
            item["FlagA"] = "/_layouts/images/hhelp.gif";
            item["TeamB"] = "Group C Runner-up";
            item["FlagB"] = "/_layouts/images/hhelp.gif";
            item["MatcheDate"] =DateTime.Parse("2012-06-24T18:45:00Z");
            item.Update();


            //semi

            item = list.Items.Add();
            item["TeamA"] = "Quarter Final 1 Winner";
            item["FlagA"] = "/_layouts/images/hhelp.gif";
            item["TeamB"] = "Quarter Final 3 Winner";
            item["FlagB"] = "/_layouts/images/hhelp.gif";
            item["MatcheDate"] = DateTime.Parse("2012-06-27T18:45:00Z");
            item.Update();

            item = list.Items.Add();
            item["TeamA"] = "Quarter Final 2 Winner";
            item["FlagA"] = "/_layouts/images/hhelp.gif";
            item["TeamB"] = "Quarter Final 4 Winner";
            item["FlagB"] = "/_layouts/images/hhelp.gif";
            item["MatcheDate"] = DateTime.Parse("2012-06-28T18:45:00Z");
            item.Update();


            //final
            item = list.Items.Add();
            item["TeamA"] = "Semi Final 1 Winner";
            item["FlagA"] = "/_layouts/images/hhelp.gif";
            item["TeamB"] = "Semi Final 2 Winner";
            item["FlagB"] = "/_layouts/images/hhelp.gif";
            item["MatcheDate"] = DateTime.Parse("2012-07-01T18:45:00Z");
            item.Update();
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
